1
|
|
|
import { |
2
|
|
|
DynamicModule, |
3
|
|
|
FactoryProvider, |
4
|
|
|
Global, |
5
|
|
|
Module, |
6
|
|
|
Provider |
7
|
|
|
} from '@nestjs/common'; |
8
|
|
|
import { ModuleRef, Reflector } from '@nestjs/core'; |
9
|
|
|
|
10
|
|
|
import { ICacheRBAC } from './interfaces/cache.rbac.interface'; |
11
|
|
|
import { IDynamicStorageRbac } from './interfaces/dynamic.storage.rbac.interface'; |
12
|
|
|
import { IStorageRbac } from './interfaces/storage.rbac.interface'; |
13
|
|
|
import { RbacService } from './services/rbac.service'; |
14
|
|
|
import { StorageRbacService } from './services/storage.rbac.service'; |
15
|
|
|
|
16
|
|
|
@Global() |
17
|
|
|
@Module({ |
18
|
|
|
providers: [RbacService, StorageRbacService, Reflector], |
19
|
|
|
imports: [], |
20
|
|
|
exports: [RbacService] |
21
|
|
|
}) |
22
|
|
|
export class RbacModule { |
23
|
|
|
private static cache?: any | ICacheRBAC; |
24
|
|
|
private static cacheOptions?: { KEY?: string; TTL?: number }; |
25
|
|
|
|
26
|
|
|
static useCache( |
27
|
|
|
cache: any | ICacheRBAC, |
28
|
|
|
options?: { |
29
|
|
|
KEY?: string; |
30
|
|
|
TTL?: number; |
31
|
|
|
} |
32
|
|
|
) { |
33
|
|
|
RbacModule.cache = cache; |
34
|
|
|
RbacModule.cacheOptions = options; |
35
|
|
|
return RbacModule; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
static forRoot( |
39
|
|
|
rbac: IStorageRbac, |
40
|
|
|
providers?: any[], |
41
|
|
|
imports?: any[] |
42
|
|
|
): DynamicModule { |
43
|
|
|
return RbacModule.forDynamic( |
44
|
|
|
class { |
45
|
|
|
async getRbac(): Promise<IStorageRbac> { |
46
|
|
|
return rbac; |
47
|
|
|
} |
48
|
|
|
}, |
49
|
|
|
providers, |
50
|
|
|
imports |
51
|
|
|
); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
static forDynamic( |
55
|
|
|
rbac: new () => IDynamicStorageRbac, |
56
|
|
|
providers?: any[], |
57
|
|
|
imports?: any[] |
58
|
|
|
): DynamicModule { |
59
|
|
|
const inject = [ModuleRef, rbac]; |
60
|
|
|
const commonProviders: Provider<any>[] = []; |
61
|
|
|
if (RbacModule.cache) { |
62
|
|
|
commonProviders.push(RbacModule.cache, { |
63
|
|
|
provide: 'ICacheRBAC', |
64
|
|
|
useFactory: (cache: ICacheRBAC): ICacheRBAC => { |
65
|
|
|
return RbacModule.setCacheOptions(cache); |
66
|
|
|
}, |
67
|
|
|
inject: [RbacModule.cache] |
68
|
|
|
}); |
69
|
|
|
inject.push(RbacModule.cache); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
const storageRbacService: FactoryProvider = { |
73
|
|
|
provide: StorageRbacService, |
74
|
|
|
useFactory: async ( |
75
|
|
|
moduleRef: ModuleRef, |
76
|
|
|
rbacService: IDynamicStorageRbac, |
77
|
|
|
cache?: ICacheRBAC |
78
|
|
|
) => { |
79
|
|
|
return new StorageRbacService( |
80
|
|
|
moduleRef, |
81
|
|
|
rbacService, |
82
|
|
|
RbacModule.setCacheOptions(cache) |
83
|
|
|
); |
84
|
|
|
}, |
85
|
|
|
inject |
86
|
|
|
}; |
87
|
|
|
|
88
|
|
|
commonProviders.push(...[...(providers || []), rbac], storageRbacService); |
89
|
|
|
|
90
|
|
|
return { |
91
|
|
|
module: RbacModule, |
92
|
|
|
providers: commonProviders, |
93
|
|
|
imports: [...(imports || [])] |
94
|
|
|
}; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
private static setCacheOptions(cache?: ICacheRBAC) { |
98
|
|
|
if (!cache || RbacModule.cacheOptions) { |
99
|
|
|
return cache; |
100
|
|
|
} |
101
|
|
|
if (!RbacModule.cacheOptions) { |
102
|
|
|
return cache; |
103
|
|
|
} |
104
|
|
|
if (RbacModule.cacheOptions.KEY) { |
105
|
|
|
cache.KEY = RbacModule.cacheOptions.KEY; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
if (RbacModule.cacheOptions.TTL) { |
109
|
|
|
cache.TTL = RbacModule.cacheOptions.TTL; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
return cache; |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|